package com.ssm.car.server.controller.parkingzone;


import com.jee.ssm.common.support.Const;
import com.jee.ssm.common.utils.UUIDFactory;
import com.jee.ssm.common.web.json.Tip;
import com.jee.ssm.common.web.param.ParamMap;
import com.ssm.car.dao.model.Parkingzone;
import com.ssm.car.rpc.api.parking.ParkingService;
import com.ssm.car.rpc.api.parkinglot.ParkinglotService;
import com.ssm.car.rpc.api.parkingzone.ParkingzoneService;
import com.ssm.upms.dao.model.Account;
import com.ssm.upms.log.AdminControllerLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.Date;
import java.util.List;

/**
* 停车场分区表管理 Controller
* @author GaoXiang
* @version 1.0
*/
@Controller
@RequestMapping("/parkingzone")
public class ParkingzoneController  {


    /**
     * 进入停车场分区表添加页面 携带一个生成的id --> longId
     * @param model 返回的实体容器
     * @return 添加页面
     */
    @RequestMapping(value="/add")
    @RequiresPermissions("parkingzone:add")
    public String add(ModelMap model, HttpServletRequest request){
        HttpSession session = request.getSession();
     /*   Account account = (Account)session.getAttribute(Const.ACCOUNT);
        if (account.getType() == 6){
            Parking parking = parkingService.selectById(account.getInfoId());
            model.put("parking",parking);
            model.put("floor",parking.getFloor());
               model.put("type",account.getType());
        }*/

        model.put("longId", UUIDFactory.getStringId());
        return "manager/parkingzone/add";
    }


    /**
     * 进入停车场分区表编辑页面
     * @param model 返回停车场分区表的容器
     * @param id 停车场分区表id
     * @return 编辑页面
     */
    @RequestMapping(value="/edit")
    @RequiresPermissions("parkingzone:edit")
    public String edit(ModelMap model, @RequestParam String id, HttpServletRequest request) {
      /*  Parkingzone pz= parkingzoneService.selectById(id);
        HttpSession session= request.getSession();
        Account account= (Account) session.getAttribute(Const.ACCOUNT);
        Parking pp= parkingService.selectById(pz.getParkingId());
        pz.setParking(pp);
        model.put("floor",pp.getFloor());
        model.put("type",account.getType());
        model.put("data",pz);
        Image image=new Image();
        image.setInfoId(id);
        image.setType("5");
        Image  imageList= imageService.selectByInfoId5(image);
        if(imageList!=null){
            model.put("imageList", imageList);
        }
        image.setInfoId(id);
        image.setType("6");
        Image  imageList2= imageService.selectByInfoId5(image);
        if(imageList2!=null){
            model.put("imageList2", imageList2);
        }*/
        return "manager/parkingzone/edit";
    }


    /**
     * 停车场分区表添加
     * @param parkingzone 带id的停车场分区表对象
     * @return 成功状态
     */
    @RequestMapping(value="/save")
    @ResponseBody
    @RequiresPermissions("parkingzone:add")
    @AdminControllerLog(description = "添加停车场分区表" )
    public Tip save(Parkingzone parkingzone)  {
        try {
            parkingzone.setTime(new Date());
            parkingzoneService.insert(parkingzone);
            return new Tip();
        } catch (Exception e) {
            //e.printStackTrace();
            return new Tip(1,"添加失败！");
        }
    }


    /**
     * 根据 id 修改停车场分区表
     * @param parkingzone 带id的停车场分区表对象
     * @return 成功状态
     */
    @RequestMapping(value="/update")
    @ResponseBody
    @RequiresPermissions("parkingzone:edit")
    @AdminControllerLog(description = "修改停车场分区表" )
    public Tip update(Parkingzone parkingzone) {

        try {
            parkingzoneService.updateById(parkingzone);
            return new Tip();
        } catch (Exception e) {
            //e.printStackTrace();
            return new Tip(1,"修改失败！");
        }

    }


    /**
     * 根据 id 删除停车场分区表
     * @param id 停车场分区表id
     * @return 成功状态
     */
    @RequestMapping(value="/delete")
    @ResponseBody
    @RequiresPermissions("parkingzone:delete")
    @AdminControllerLog(description = "删除停车场分区表" )
    public Tip delete(@RequestParam String id) {

        try {
       /*     Parkingzone  pz= parkingzoneService.selectById(id);
            if(pz!=null){
                if(pz.getImg()!=null && pz.getImg()!=""){
                    imageService.deleteById(pz.getImg());
                }
                if(pz.getPhoto()!=null && pz.getPhoto()!=""){
                    imageService.deleteById(pz.getPhoto());
                }
            }*/
            parkingzoneService.deleteById(id);
            return new Tip();
        } catch (Exception e) {
            //e.printStackTrace();
            return new Tip(1,"删除失败！");
        }

    }


    /**
     * 根据 id 列表批量删除
     * @param ids 停车场分区表id List
     * @return 成功状态
     */
    @RequestMapping(value="/deleteByIds")
    @ResponseBody
    @RequiresPermissions("parkingzone:delete")
    @AdminControllerLog(description = "批量删除停车场分区表" )
    public Tip deleteByIds(@RequestParam("ids") List<String> ids) {

        try {
            parkingzoneService.deleteByIds(ids);
            return new Tip();
        } catch (Exception e) {
            //e.printStackTrace();
            return new Tip(1,"批量删除失败！");
        }

    }


    /**
     * 根据 id 查找停车场分区表
     * @param id 停车场分区表id
     * @return 停车场分区表对象 json
     */
    @RequestMapping(value="/findJson")
    @ResponseBody
    @RequiresPermissions("parkingzone:list")
    public Parkingzone find(@RequestParam String id) {

        return parkingzoneService.selectById(id);
    }


    /**
     * 获取停车场分区表列表 获取全部 不分页
     * @param request 请求参数
     * @return 停车场分区表列表页面
     */
    @RequestMapping(value="/all")
    @RequiresPermissions("parkingzone:list")
    public String all(HttpServletRequest request, ModelMap modelMap) {

        return list(request,modelMap,1,0);
    }


    /**
     * 获取停车场分区表列表 分页
     * @param request 请求参数
     * @param page 第几页
     * @param size 每页大小
     * @return 停车场分区表列表页面
     */
    @RequestMapping(value="/list")
    @RequiresPermissions("parkingzone:list")
    public String list(HttpServletRequest request, ModelMap modelMap, Integer page, Integer size) {
        HttpSession session = request.getSession();
        Account account = (Account)session.getAttribute(Const.ACCOUNT);
        ParamMap paramMap = new ParamMap(request);
        if(account.getType() == 6) {
            paramMap.put("parkingId", account.getInfoId());
        }if(account.getType() == 7) {
            paramMap.put("parkingId", account.getInfoId());
        }
        modelMap.put("type",account.getType());
        modelMap.put("pageInfo",parkingzoneService.list(paramMap,page,size));
        modelMap.putAll(new ParamMap(request));
        return "manager/parkingzone/list";
    }

    /**
     * 上传地图
     * @param parkingzoneId
     * @param file
     * @param request
     * @return
     */
 /*   @RequestMapping(value = "/houseImages")
    @ResponseBody
    public Tip houseImages(@RequestParam String parkingzoneId, @RequestParam(name = "house") MultipartFile file, HttpServletRequest request){
        try {
            String filename = file.getOriginalFilename();
            String name = UUIDFactory.getStringId() + "_" + System.currentTimeMillis() + filename.substring(filename.lastIndexOf("."));
            String url = "/images/parkingzone/" + name;
            String realPath = request.getSession().getServletContext().getRealPath(url);
            File img = new File(realPath);
            if (!img.exists() && !img.isDirectory()) {
                img.mkdirs();
            }
            file.transferTo(img);
            Image image=new Image();
            image.setId(UUIDFactory.getStringId());
            image.setCreateTime(new Date());
            image.setType("5");
            image.setStatus("0");
            image.setUrl(url);
            image.setInfoId(parkingzoneId);
            image.setIntro("停车场分区地图");
            imageService.deleteByInfoId5(parkingzoneId);
            parkingService.insertImg(image);
            return new Tip(image);

        } catch (Exception e) {
            e.printStackTrace();
            return new Tip(2);
        }
    }
*/

    /**
     * 上传地图
     * @param parkingzoneId
     * @param file
     * @param request
     * @return
     */
/*    @RequestMapping(value = "/houseImages2")
    @ResponseBody
    public Tip houseImages2(@RequestParam String parkingzoneId, @RequestParam(name = "house") MultipartFile file, HttpServletRequest request){
        try {
            String filename = file.getOriginalFilename();
            String name = UUIDFactory.getStringId() + "_" + System.currentTimeMillis() + filename.substring(filename.lastIndexOf("."));
            String url = "/images/parkingzone/" + name;
            String realPath = request.getSession().getServletContext().getRealPath(url);
            File img = new File(realPath);
            if (!img.exists() && !img.isDirectory()) {
                img.mkdirs();
            }
            file.transferTo(img);
            Image image=new Image();
            image.setId(UUIDFactory.getStringId());
            image.setCreateTime(new Date());
            image.setType("6");
            image.setStatus("0");
            image.setUrl(url);
            image.setInfoId(parkingzoneId);
            image.setIntro("分区停车位地图");
            imageService.deleteByInfoId6(parkingzoneId);
            parkingService.insertImg(image);
            return new Tip(image);

        } catch (Exception e) {
            e.printStackTrace();
            return new Tip(2);
        }
    }*/

    /**
     * 根据 id 删除图片
     * @param id 图片id
     * @return 成功状态
     */
 /*   @RequestMapping(value="/deleteImage")
    public void deleteImage(@RequestParam String id) {
        try {
            imageService.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }*/
    //---------------------------- property -------------------------------

    @Resource
    private ParkingzoneService parkingzoneService;
    @Resource
    private ParkinglotService parkinglotService;

    @Resource
    private ParkingService parkingService;
/*    @Resource
    private ImageService imageService;*/
}
